package com.jvup.gameshow.model.viewModel.impl;

import android.view.View;

import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
import androidx.lifecycle.MutableLiveData;

import com.jvup.common.support.base.BaseViewModel;
import com.jvup.gameshow.R;
import com.jvup.gameshow.databinding.LayoutImagePickerAlbumItemBinding;
import com.jvup.gameshow.databinding.LayoutImagePickerPictureItemBinding;
import com.jvup.gameshow.databinding.LayoutImagePickerSelectedItemBinding;
import com.jvup.gameshow.model.entity.ImageFloder;
import com.jvup.gameshow.model.entity.ImageItem;
import com.jvup.common.widget.adapter.auto.AutoVariable;
import com.jvup.common.widget.adapter.auto.SummerBindAdapter;
import com.jvup.common.widget.adapter.auto.SummerRecyclerBindAdapter;
import com.jvup.common.widget.adapter.auto.annotation.AdapterEntityBind;
import com.jvup.gameshow.view.attributeBinder.attribute.ImageAttribute;

import java.util.ArrayList;

public class ImagePickerViewModel extends BaseViewModel {

    public final int min_limit=1;
    public       int max_limit=9;

    public final ArrayList<AlbumListItemModel> folderDataList       = new ArrayList();
    public final ArrayList<GridPictureItemModel> pictureDataList    = new ArrayList();
    public final ArrayList<SelectBitmapItemModel> selectDataList    = new ArrayList();

    public final SummerBindAdapter folderAdapter    = new SummerBindAdapter(folderDataList);
    public final SummerBindAdapter pictureAdapter   = new SummerBindAdapter(pictureDataList);
    public final SummerRecyclerBindAdapter selectAdapter = new SummerRecyclerBindAdapter(selectDataList);

    public final ImageFloder imageAll = new ImageFloder();
    public final ArrayList<ImageFloder> mDirPaths = new ArrayList<>();

    private ImageFloder currentImageFolder = imageAll;

    public void ImagePickerViewModel(){
        put("albumTitle", "图片");
        put("chooseHintText", String.format("已选择%d张照片(最多%d张)", selectDataList.size(), max_limit));
    }

    public void setMaxLimit(int max){
        max_limit = max;
    }

    public boolean checkLimit(){
        return selectDataList.size() + 1 > max_limit;
    }

    public void addSelectBitmapItem(GridPictureItemModel itemModel){
        selectDataList.add(new SelectBitmapItemModel(itemModel));
        selectAdapter.notifyDataSetChanged();
        itemModel.checkSelected = true;
        pictureAdapter.notifyDataSetChanged();
        updateChooseHintText();
    }

    public void removeSelectBitmapItem(GridPictureItemModel itemModel){
        selectDataList.remove(new SelectBitmapItemModel(itemModel));
        itemModel.checkSelected=false;
        selectAdapter.notifyDataSetChanged();
        pictureAdapter.notifyDataSetChanged();
        updateChooseHintText();
    }

    public void removeSelectBitmapItem(int position) {
        selectDataList.remove(position).gridPictureItemModel.checkSelected=false;
        selectAdapter.notifyDataSetChanged();
        pictureAdapter.notifyDataSetChanged();
        updateChooseHintText();
    }

    public void setCurrentImageFolder(ImageFloder currentImageFolder){
        this.currentImageFolder = currentImageFolder;
        pictureDataList.clear();
        for(ImageItem item : currentImageFolder.images){
            pictureDataList.add(new GridPictureItemModel("file://" + item.path,false));
//            pictureDataList.add(new GridPictureItemModel(item.path,false));
        }
        pictureAdapter.notifyDataSetChanged();
    }
    public ImageFloder getCurrentImageFolder(){
        return currentImageFolder;
    }

    public void updateChooseHintText() {
        put("chooseHintText", String.format("已选择%d张照片(最多%d张)", selectDataList.size(), max_limit));
    }
    public void updateAlbumTitle(String albumTitle){
        put("albumTitle", albumTitle);
    }

    public MutableLiveData<CharSequence> getChooseHintText() {
        return get("chooseHintText");
    }
    public MutableLiveData<CharSequence> getAlbumTitle() {
        return get("albumTitle");
    }

    //----------------------------------------------------------------------------------------------
    @AdapterEntityBind(viewRid= R.layout.layout_image_picker_album_item)
    public static class AlbumListItemModel extends BaseObservable implements AutoVariable<LayoutImagePickerAlbumItemBinding> {

        private final ImageAttribute dirItemImage;
        private final String dirItemName;
        private int chooseVisible = View.VISIBLE;
        private int itemBottomLineVisible = View.VISIBLE;

        public AlbumListItemModel(String dirItemImage, String dirItemName, int itemBottomLineVisible, int chooseVisible){
            this.dirItemImage = new ImageAttribute(dirItemImage);
            this.dirItemName = dirItemName;
            this.itemBottomLineVisible = itemBottomLineVisible;
            this.chooseVisible = chooseVisible;
        }

        public void setChooseVisible(int chooseVisible) {
            this.chooseVisible = chooseVisible;
        }

        @Override
        public void bindVariable(LayoutImagePickerAlbumItemBinding viewBind) {
            viewBind.setAlbumListItemModel(this);
        }

        @Bindable
        public ImageAttribute getDirItemImage() {
            return dirItemImage;
        }
        @Bindable
        public CharSequence getDirItemName() {
            return dirItemName;
        }
        @Bindable
        public int getChooseVisible(){
            return chooseVisible;
        }
        @Bindable
        public int getItemBottomLineVisible(){
            return itemBottomLineVisible;
        }
    }

    @AdapterEntityBind(viewRid= R.layout.layout_image_picker_picture_item)
    public static class GridPictureItemModel extends BaseObservable implements AutoVariable<LayoutImagePickerPictureItemBinding>{

        private final ImageAttribute pictureItem;
        private boolean checkSelected = false;

        public GridPictureItemModel(String pictureItem, boolean checkSelected){
            this.pictureItem = new ImageAttribute(pictureItem);
            this.checkSelected = checkSelected;
        }

        @Override
        public void bindVariable(LayoutImagePickerPictureItemBinding viewBind) {
            viewBind.setGridPictureItemModel(this);
        }

        public boolean checkSelected(){
            return checkSelected;
        }


        @Bindable
        public ImageAttribute getPictureItem(){
            return pictureItem;
        }

        @Bindable
        public int getSelectedVisible(){
            return checkSelected ? View.VISIBLE: View.GONE;
        }

    }

    @AdapterEntityBind(viewRid= R.layout.layout_image_picker_selected_item)
    public static class SelectBitmapItemModel extends BaseObservable implements AutoVariable<LayoutImagePickerSelectedItemBinding>{
        public final GridPictureItemModel gridPictureItemModel;
        public ImageAttribute itemSelectImage;

        public SelectBitmapItemModel(GridPictureItemModel itemModel){
            this.gridPictureItemModel   = itemModel;
            this.itemSelectImage        = itemModel.pictureItem;
        }

        @Override
        public boolean equals(Object obj) {
            if(obj instanceof String)
                return itemSelectImage.getUrl().equals(obj);
            if(obj instanceof ImageAttribute)
                return itemSelectImage.equals(obj);
            if(obj instanceof GridPictureItemModel)
                return gridPictureItemModel.equals(obj);
            if(obj instanceof SelectBitmapItemModel)
                return itemSelectImage.equals(((SelectBitmapItemModel) obj).itemSelectImage);
            return super.equals(obj);
        }

        @Override
        public void bindVariable(LayoutImagePickerSelectedItemBinding viewBind) {
            viewBind.setSelectBitmapItemModel(this);
        }

        @Bindable
        public ImageAttribute getItemSelectImage() {
            return itemSelectImage;
        }

    }

}
